<?php

namespace Home\Controller;
use Think\Controller;

class UploadController extends Controller {


	
   public function upOtherCard() {

        $upload = new \Think\Upload();// 实例化上传类
        $upload->maxSize   =     3145728000 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型     
        $upload->savePath = '/othercard/';

        $info=$upload->upload();
        $imags   = json_encode($info); 
        session('up_imgs',$imags);
        echo $imags; 

   }

   public function deleteCertificates() {

        if (IS_POST) {
           $imgid = trim(I('post.imgid', '', 'htmlspecialchars'));
        $otherimages=json_decode(session('up_imgs'),true);
       
        if ($imgid) {
          $key=$imgid-1;
          unset($otherimages[$key]); 
        $up_imgs=json_encode($otherimages);
         session('up_imgs',$up_imgs); 
        }
        }
    }


    function addFileToZip($path,$zip) {
        $handler = opendir($path); //打开当前文件夹由$path指定。
        /*
        循环的读取文件夹下的所有文件和文件夹
        其中$filename = readdir($handler)是每次循环的时候将读取的文件名赋值给$filename，
        为了不陷于死循环，所以还要让$filename !== false。
        一定要用!==，因为如果某个文件名如果叫'0'，或者某些被系统认为是代表false，用!=就会停止循环
        */
        while (($filename = readdir($handler)) !== false) {
        if ($filename != "." && $filename != "..") {
        if (is_dir($path . "/" . $filename)) {
      $this->addFileToZip($path . "/" . $filename, $zip);
        } else { 
        $zip->addFile($path . "/" . $filename);
        }
        }
        }
        @closedir($path);
        }


        public function zip()
        {
                 $zip = new \ZipArchive();
if ($zip->open('images.zip', \ZipArchive::OVERWRITE) === TRUE) {
$this->addFileToZip('Uploads/othercard/', $zip); //调用方法，对要打包的根目录进行操作，并将ZipArchive的对象传递给方法
$zip->close(); //关闭处理的zip文件
}
$filename='images.zip';

header("Cache-Control: public"); 
header("Content-Description: File Transfer"); 
header('Content-disposition: attachment; filename='.basename($filename)); //文件名   
header("Content-Type: application/force-download");
header("Content-Transfer-Encoding: binary");
header('Content-Length: '. filesize($filename)); //告诉浏览器，文件大小   
readfile($filename);
//header('Location:/images.zip');
        }





}
